package com.wisdom.smartbook.facelib.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.wisdom.smartbook.facelib.core.FaceCheckManager;
import com.wisdom.smartbook.facelib.core.model.FaceDetectInfo;
import com.wisdom.smartbook.utils.KLog;

import java.util.ArrayList;
import java.util.List;

/**
 * 用于显示人脸信息的控件
 */
public class FaceRectViewNew extends View {
    private Rect faceRect = new Rect(0, 0, 0, 0);

    // 画笔，复用
    private Paint paint;

    // 默认人脸框厚度
    private static final int DEFAULT_FACE_RECT_THICKNESS = 5;

    public FaceRectViewNew(Context context) {
        this(context, null);
    }

    public FaceRectViewNew(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (faceRect != null) {
            drawFaceRect(canvas, faceRect, DEFAULT_FACE_RECT_THICKNESS, paint);
        }
    }

    public void clearFaceInfo() {
        faceRect = null;
        postInvalidate();
    }

    public void addFaceInfo(Rect faceDetect) {
        faceRect = faceDetect;
        postInvalidate();
    }


    /**
     * 绘制数据信息到view上，若 {@link DrawInfo#getName()} 不为null则绘制 {@link DrawInfo#getName()}
     *
     * @param canvas            需要被绘制的view的canvas
     * @param rect              绘制信息
     * @param faceRectThickness 人脸框厚度
     * @param paint             画笔
     */
    public static void drawFaceRect(Canvas canvas, Rect rect, int faceRectThickness, Paint paint) {
        if (canvas == null || rect == null) {
            return;
        }


        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(faceRectThickness);
        paint.setColor(Color.GREEN);
        paint.setAntiAlias(true);

        Path mPath = new Path();
//        // 左上
////        Rect rect = new Rect(drawInfo.left, drawInfo.top, drawInfo.right, drawInfo.bottom);
//        Rect rect = new Rect(drawInfo.top, drawInfo.left, drawInfo.bottom, drawInfo.right);
//
//        KLog.d("人脸位置"+ drawInfo.toString());
        mPath.moveTo(rect.left, rect.top + rect.height() / 4);
        mPath.lineTo(rect.left, rect.top);
        mPath.lineTo(rect.left + rect.width() / 4, rect.top);
        // 右上
        mPath.moveTo(rect.right - rect.width() / 4, rect.top);
        mPath.lineTo(rect.right, rect.top);
        mPath.lineTo(rect.right, rect.top + rect.height() / 4);
        // 右下
        mPath.moveTo(rect.right, rect.bottom - rect.height() / 4);
        mPath.lineTo(rect.right, rect.bottom);
        mPath.lineTo(rect.right - rect.width() / 4, rect.bottom);
        // 左下
        mPath.moveTo(rect.left + rect.width() / 4, rect.bottom);
        mPath.lineTo(rect.left, rect.bottom);
        mPath.lineTo(rect.left, rect.bottom - rect.height() / 4);
        canvas.drawPath(mPath, paint);

        // 绘制文字，用最细的即可，避免在某些低像素设备上文字模糊
        paint.setStrokeWidth(1);
//
//        if (drawInfo.getName() == null) {
//            paint.setStyle(Paint.Style.FILL_AND_STROKE);
//            paint.setTextSize(rect.width() / 8);
//
//            String str = (drawInfo.getSex() == GenderInfo.MALE ? "MALE" : (drawInfo.getSex() == GenderInfo.FEMALE ? "FEMALE" : "UNKNOWN"))
//                    + ","
//                    + (drawInfo.getAge() == AgeInfo.UNKNOWN_AGE ? "UNKNOWN" : drawInfo.getAge())
//                    + ","
//                    + (drawInfo.getLiveness() == LivenessInfo.ALIVE ? "ALIVE" : (drawInfo.getLiveness() == LivenessInfo.NOT_ALIVE ? "NOT_ALIVE" : "UNKNOWN"));
//            canvas.drawText(str, rect.left, rect.top - 10, paint);
//        } else {
//            paint.setStyle(Paint.Style.FILL_AND_STROKE);
//            paint.setTextSize(rect.width() / 8);
//            canvas.drawText(drawInfo.getName(), rect.left, rect.top - 10, paint);
//        }
    }


}